package edu.ynu.se.xiecheng.achitectureclass.dao;

import edu.ynu.se.xiecheng.achitectureclass.common.dao.LogicDAO;
import edu.ynu.se.xiecheng.achitectureclass.entity.Business;
import edu.ynu.se.xiecheng.achitectureclass.entity.Customer;
import edu.ynu.se.xiecheng.achitectureclass.entity.Order;
import edu.ynu.se.xiecheng.achitectureclass.entity.Shop;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface BusinessDao extends LogicDAO<Business,Long> {

    @Modifying
    @Query(value = "INSERT INTO shop (name, address) VALUES (:name, :address)", nativeQuery = true)
    void addShop(@Param("name") String name, @Param("address") String address);

    @Modifying
    @Query(value = "INSERT INTO shop_item (business_id, item_id, on_sale) VALUES (:businessId, :itemId, false)", nativeQuery = true)
    void addItemToShop(@Param("businessId") Long businessId, @Param("itemId") Long itemId);

    List<Order> getOrdersByShop(Shop shop);

    @Transactional
    @Modifying
    @Query("UPDATE Orders o SET o.isCancelled = '已取消' WHERE o.isPaid = true AND o.customer = :customer")
    void cancelOrder(@Param("customer") Customer customer);

    @Transactional
    @Modifying
    @Query("UPDATE Orders o SET o.isConfirmed = '已确认' WHERE o.isPaid = true AND o.shop = :shop")
    void confirmOrder(@Param("shop") Shop shop);

    @Transactional
    @Modifying
    @Query("UPDATE Orders o SET o.isDeleted = '已退款' WHERE o.isPaid =true AND o.shop = :shop")
    void refundOrder(@Param("shop") Shop shop);

    @Query("SELECT o.shop FROM Orders o WHERE o.shop.id = :shopId")
    Shop getShop(@Param("shopId") Long shopId);
}

